/*
 * @Author: tomystery 
 * @Date: 2018-05-14 14:30:52 
 * @Last Modified by: t
 * @Last Modified time: 2018-07-13 16:50:25
 */
import MUtil from 'util/mm.jsx'
const _mm = new MUtil()
class Product {

    getProductList(listParam) {

        let url = '';
        let data = {};
        if (listParam.listType === 'list') {
            url = '/manage/product/list.do';
            data.pageNum = listParam.pageNum;
        } else if (listParam.listType === 'search') {
            url = '/manage/product/search.do';
            data.pageNum = listParam.pageNum;
            data[listParam.searchType] = listParam.keyword;
        }

        return _mm.request({
            type: 'post',
            url: url,
            data: data
        })
    }
    getProduct(productId) {
        return _mm.request({
            type: 'post',
            url: '/manage/product/detail.do',
            data: {
                productId: productId || 0
            }
        })
    }

    setSaleStatus(productInfo) {
        return _mm.request({
            type: 'post',
            url: ' /manage/product/set_sale_status.do',
            data: productInfo
        })
    }
    getFirstCategory(categoryInfo) {
        return _mm.request({
            type: 'get',
            url: ' /manage/category/get_category.do',
            data: categoryInfo
        })

    }


    saveProduct(product) {
        return _mm.request({
            type: 'post',
            url: '/manage/product/save.do',
            data: product
        });
    }


    checkProduct(product) {
        let result = {
            status: true,
            msg: '通过验证'
        };
        //判断用户名为空
        if (typeof product.name !== 'string' || product.name.length === 0) {
            return {
                status: false,
                msg: '商品名称不能为空！'
            }
        };
        //判断描述不能为空
        if (typeof product.subtitle !== 'string' || product.subtitle.length === 0) {
            return {
                status: false,
                msg: '商品描述不能为空!'
            }
        }
       
        if (typeof product.price !== 'number' || !(product.price >= 0)) {
            return {
                status: false,
                msg: "请输入正确的商品价格！"
            }
        }
        
        if (typeof product.stock !== 'number' || !(product.stock >= 0)) {
            return {
                status: false,
                msg: "请输入正确的库存数量！"
            }
        }
        return result;
    }

    
    getProductCategoryList(categoryId) {
        return _mm.request({
            type: 'get',
            url: '/manage/category/get_category.do',
            data: {
                categoryId:categoryId
            }
        })

    }

   
    updateCategoryName(categoryId,categoryName){
        return _mm.request({
            type: 'get',
            url: '/manage/category/set_category_name.do',
            data: {
                categoryId:categoryId,
                categoryName:categoryName
            }
        })
    }

    //增加品类
   
    addCategory(category){
        return _mm.request({
            type: 'post',
            url: '/manage/category/add_category.do',
            data: category
        })
    }
}
export default Product